package com.ruicar.afs.cloud.apply.pre.loan.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.apply.common.dto.ProductInfoDTO;
import com.ruicar.afs.cloud.apply.common.entity.*;
import com.ruicar.afs.cloud.apply.common.feign.Apply2ProductFeign;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConfig;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.common.utils.ApplyUtils;
import com.ruicar.afs.cloud.apply.pre.loan.condition.ApplyReportCondition;
import com.ruicar.afs.cloud.apply.pre.loan.condition.AssetChangeCondition;
import com.ruicar.afs.cloud.apply.pre.loan.condition.CarInfoCondition;
import com.ruicar.afs.cloud.apply.pre.loan.mapper.ApplyModifyHistoryMapper;
import com.ruicar.afs.cloud.apply.pre.loan.service.*;
import com.ruicar.afs.cloud.apply.pre.loan.vo.AssetChangeVo;
import com.ruicar.afs.cloud.apply.pre.loan.vo.AssetContentVO;
import com.ruicar.afs.cloud.apply.pre.loan.vo.CarInfoVO;
import com.ruicar.afs.cloud.bizcommon.algorithm.enums.CalculateType;
import com.ruicar.afs.cloud.bizcommon.business.bean.FinanceCalculatorBean;
import com.ruicar.afs.cloud.bizcommon.business.bean.FinanceInfoBean;
import com.ruicar.afs.cloud.bizcommon.business.dto.CalculatorDTO;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinCostDetails;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinDiscountDetails;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinFinancingItems;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinRentAdjustDetails;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyCostDetailsService;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.uid.UidGenerator;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.CaseCarStyleDetailDto;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleDetail;
import com.ruicar.afs.cloud.parameter.commom.entity.TsysVehicleModel;
import com.ruicar.afs.cloud.parameter.commom.enums.*;
import com.ruicar.afs.cloud.parameter.commom.service.TsysVehicleDetailService;
import com.ruicar.afs.cloud.parameter.commom.service.TsysVehicleModelService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.*;

/**
 * 资产变更实现类
 *
 * @author 耿继辉
 */
@AllArgsConstructor
@Service
public class ApplyModifyHistoryServiceImpl extends ServiceImpl<ApplyModifyHistoryMapper, ApplyModifyHistory> implements ApplyModifyHistoryService {

    private TsysVehicleDetailService vehicleDetailService;
    private ApplyOrderInfoService orderInfoService;
    private ApplyCarDetailsService carDetailsService;
    private UidGenerator uidGenerator;
    private ApplyCostDetailsService costDetailsService;
    private TsysVehicleDetailService tsysVehicleDetailService;
    private TsysVehicleModelService tsysVehicleModelService;
    private Apply2ProductFeign productFeign;
    private ApplyConfig config;
    private ApplyCustBaseInfoService applyCustBaseInfoService;
    private ApplyOprRecordService applyOprRecordService;

    /**
     * 获取产品请求头
     *
     * @return
     */
    public Map makeProductHeader() {
        Map<String, String> headers = new HashMap<>();
        headers.put("clientId", config.getProductClientId());
        headers.put("clientSecret", config.getProductClientSecret());
        return headers;
    }

    /**
     * 获取资产变更历史数据
     * @param applyNo
     * @return
     */
    public ApplyModifyHistory getModifyHistoryByApplyNo(String applyNo){

        List<ApplyModifyHistory> list = this.baseMapper.selectList(Wrappers.<ApplyModifyHistory>query().lambda().eq(ApplyModifyHistory::getApplyNo,applyNo).orderByDesc(ApplyModifyHistory::getCreateTime));
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    /**
     * 版本号为空-查询历史版本号
     * @param applyNo
     * @return
     */
    public ApplyModifyHistory getModifyHistory(String applyNo){

        return this.baseMapper.getModifyHistory(applyNo);
    }

    /**
     * 获取最大版本号
     * @param applyNo
     * @return
     */
    public Integer getMaxVersion(String applyNo){

        return this.baseMapper.getMaxVersion(applyNo);
    }


    /**
     * 车辆明细
     * @param applyNo
     * @param condition
     * @return
     */
    public ApplyCarDetails finCarDetailsList(String applyNo, CarInfoCondition condition){

        ApplyCarDetails carDetails = new ApplyCarDetails();
        if (CarNature.NEW_CAR.getIndex().equals(condition.getCarNature())) {
            carDetails.setBrandId(condition.getBrandId());
            carDetails.setBrandName(condition.getBrandName());
            carDetails.setModelId(condition.getModelId());
            carDetails.setModelName(condition.getModelName());
            carDetails.setStyleId(condition.getStyleId());
            carDetails.setStyleName(condition.getStyleName());
            carDetails.setGuidePrice(condition.getGuidePrice());
            carDetails.setSalePrice(condition.getSalePrice());
            carDetails.setPurchaseProvince(condition.getPurchaseProvince());
            carDetails.setPurchaseCity(condition.getPurchaseCity());
            carDetails.setLicenseProvince(condition.getLicenseProvince());
            carDetails.setLicenseCity(condition.getLicenseCity());
            // 获取车型库完整信息
            TsysVehicleDetail vehicleDetail = vehicleDetailService.getVehicleDetail(condition.getStyleId(),condition.getModelId());
            if (vehicleDetail != null) {
                carDetails.setIsInternet(vehicleDetail.getIsInternet());
                carDetails.setIsGreen(vehicleDetail.getIsGreen());
                carDetails.setSeatNum(vehicleDetail.getSeatNum());
                carDetails.setConfigure(vehicleDetail.getConfigure());
                carDetails.setYearStyle(vehicleDetail.getYearStyle());
                carDetails.setDriver(vehicleDetail.getDriver());
                carDetails.setBlowoff(vehicleDetail.getBlowoff());
                carDetails.setCarTypeMain(vehicleDetail.getCarType());
                carDetails.setCarTypeDetail(vehicleDetail.getCarTypeDetail());
                carDetails.setCarBodyClass(vehicleDetail.getCarLevel());
                carDetails.setCarTransmission(vehicleDetail.getTransmission());
                carDetails.setCarDisplacement(vehicleDetail.getDisplacement());
            }
        } else if (CarNature.PLATE_CAR.getIndex().equals(condition.getCarNature())) {
            carDetails.setCarVin(condition.getCarVin());
            carDetails.setBrandId(condition.getBrandId());
            carDetails.setBrandName(condition.getBrandName());
            carDetails.setModelId(condition.getModelId());
            carDetails.setModelName(condition.getModelName());
            carDetails.setStyleId(condition.getStyleId());
            carDetails.setStyleName(condition.getStyleName());
            carDetails.setGuidePrice(condition.getGuidePrice());
            carDetails.setSalePrice(condition.getSalePrice());
            carDetails.setCarFunction(condition.getCarFunction());
            carDetails.setEngineNo(condition.getEngineNo());
            carDetails.setPurchaseProvince(condition.getPurchaseProvince());
            carDetails.setPurchaseCity(condition.getPurchaseCity());
            carDetails.setLicenseProvince(condition.getLicenseProvince());
            carDetails.setLicenseCity(condition.getLicenseCity());
            carDetails.setMileage(condition.getMileage());
            if (condition.getVehicleMadeDate() != null) {
                carDetails.setVehicleMadeDate(condition.getVehicleMadeDate());
            }
            if (condition.getFirstLandingDate() != null) {
                carDetails.setFirstLandingDate(condition.getFirstLandingDate());
            }
            carDetails.setLicensePlate(condition.getLicensePlate());
            carDetails.setFirstLicenseProvince(condition.getFirstLicenseProvince());
            carDetails.setFirstLicenseCity(condition.getFirstLicenseCity());
            carDetails.setFirstLicenseAddress(condition.getFirstLicenseAddress());
            carDetails.setCarBelongs(condition.getCarBelongs());
            carDetails.setBelongsCertNo(condition.getBelongsCertNo());
            carDetails.setInvoiceTime(condition.getInvoiceTime());
            carDetails.setEvaluatingPrice(condition.getEvaluatingPrice());// 评估价格
            carDetails.setIsFirstLicense(condition.getIsFirstLicense());// 是否上次上牌
            carDetails.setTransNums(Integer.valueOf(0));// 总过户次数，默认为0
            // 获取车型库完整信息
            TsysVehicleDetail vehicleDetail = vehicleDetailService.getVehicleDetail(condition.getStyleId(),condition.getModelId());
            if (vehicleDetail != null) {
                carDetails.setIsInternet(vehicleDetail.getIsInternet());
                carDetails.setIsGreen(vehicleDetail.getIsGreen());
                carDetails.setSeatNum(vehicleDetail.getSeatNum());
                carDetails.setConfigure(vehicleDetail.getConfigure());
                carDetails.setYearStyle(vehicleDetail.getYearStyle());
                carDetails.setDriver(vehicleDetail.getDriver());
                carDetails.setBlowoff(vehicleDetail.getBlowoff());
                carDetails.setCarTypeMain(vehicleDetail.getCarType());
                carDetails.setCarTypeDetail(vehicleDetail.getCarTypeDetail());
                carDetails.setCarBodyClass(vehicleDetail.getCarLevel());
                carDetails.setCarTransmission(vehicleDetail.getTransmission());
                carDetails.setCarDisplacement(vehicleDetail.getDisplacement());
            }
        } else {
            BeanUtils.copyProperties(condition, carDetails);
            carDetails.setSalePrice(condition.getSecondSalePrice());// 二手车价格
            if(condition.getFirstLandingDate() != null){
                Long days = DateUtil.between(DateUtil.date(), condition.getFirstLandingDate(), DateUnit.DAY);
                BigDecimal vehicleAge = new BigDecimal(days).divide(new BigDecimal("360"),2,BigDecimal.ROUND_HALF_DOWN);
                carDetails.setVehicleAge(vehicleAge.toString());// 车龄
            }
            carDetails.setBrandId("");// 品牌ID
            carDetails.setModelId("");// 款式ID
            carDetails.setStyleId("");// 车型ID
        }
        carDetails.setApplyNo(applyNo);
        return carDetails;
    }

    /**
     * 融资明细
     * @param applyNo
     * @param condition
     * @return
     */
    public FinCostDetails finCostDetailsList(String applyNo, CalculatorDTO condition){

        FinCostDetails costDetails = new FinCostDetails();
        costDetails.setApplyNo(applyNo);
        costDetails.setCarId(condition.getCarId());
        costDetails.setCostType(condition.getCostType());
        costDetails.setAlgorithmType(condition.getAlgorithmType());
        costDetails.setProductId(condition.getProductId());
        costDetails.setProductName(condition.getProductName());
        costDetails.setContractAmt(condition.getContractAmt());
        costDetails.setLoanTerm(condition.getLoanTerm());
        costDetails.setCustRate(condition.getCustRate());
        costDetails.setSettleRate(condition.getSettleRate());
        costDetails.setDownPayScale(condition.getDownPayScale());
        costDetails.setDownPayAmt(condition.getDownPayAmt());
        costDetails.setIsTail(condition.getIsTail());
        costDetails.setTailPayScale(condition.getTailPayScale());
        costDetails.setTailPayAmt(condition.getTailPayAmt());
        costDetails.setDiscountType(condition.getDiscountType());
        costDetails.setDiscountAmt(condition.getDiscountAmt());
        costDetails.setMaxDiscountAmt(condition.getMaxDiscountAmt());
        costDetails.setLoanAmt(condition.getLoanAmt());
        costDetails.setTotalInterest(condition.getTotalInterest());
        costDetails.setTotalRent(condition.getTotalRent());
        costDetails.setMonthPayAmt(condition.getMonthPayAmt());
        costDetails.setIsSubProduct(condition.getIsSubProduct());
        costDetails.setAddAmt(condition.getAddAmt());
        if(ObjectUtil.isNotNull(condition.getAddPointValue())){
            costDetails.setBasicPoint(condition.getAddPointValue());
        }
        costDetails.setRepaymentMethod(condition.getRepaymentMethod());
        return costDetails;

    }

    /**
     * 融资项目明细
     * @param applyNo
     * @param carId
     * @param itemsList
     * @return
     */
    public List<FinFinancingItems> finFinancingItemsList(String applyNo, Long carId, List<FinFinancingItems> itemsList,StringBuilder sb){

        List<FinFinancingItems> outList = new ArrayList<FinFinancingItems>();
        if (itemsList != null && itemsList.size() > 0) {
            for (int i = 0; i < itemsList.size(); i++) {
                FinFinancingItems itemsCondition = itemsList.get(i);
                List<FinFinancingItems> sonList = itemsCondition.getSonList();

                FinFinancingItems financingItems = new FinFinancingItems();
                financingItems.setId(uidGenerator.getUID());
                financingItems.setApplyNo(applyNo);
                financingItems.setCarId(carId);
                financingItems.setFinanceItemCode(itemsCondition.getFinanceItemCode());
                financingItems.setFinanceItemName(itemsCondition.getFinanceItemName());
                financingItems.setFinanceItemAmt(itemsCondition.getFinanceItemAmt()!=null?itemsCondition.getFinanceItemAmt():BigDecimal.ZERO);
                financingItems.setFinanceItemTotal(itemsCondition.getFinanceItemTotal());
                if (itemsCondition.getFinanceItemTotal() >= 1 && sonList != null && sonList.size() > 0) {
                    financingItems.setIsParent(Boolean.TRUE);
                }
                // GPS、装潢、精品
                if("F003".equals(itemsCondition.getFinanceItemCode()) || "F006".equals(itemsCondition.getFinanceItemCode()) || "F007".equals(itemsCondition.getFinanceItemCode())){
                    if(itemsCondition.getFinanceItemAmt()!=null && itemsCondition.getFinanceItemAmt().compareTo(BigDecimal.ZERO)>0){
                        financingItems.setIsView(Boolean.TRUE);
                    }
                }
                sb.append(financingItems.getFinanceItemName()).append("(").append(financingItems.getFinanceItemAmt().setScale(2)).append(")");
                if (i < itemsList.size() - 1) {
                    if (sonList != null && sonList.size() > 0){
                        sb.append(":");
                    }else {
                        sb.append(",");
                    }
                }
                // 融资子项目
                if (sonList != null && sonList.size() > 0) {
                    List<FinFinancingItems> list = new ArrayList<FinFinancingItems>();
                    for (int j=0;j<sonList.size();j++) {

                        FinFinancingItems sonCondition = sonList.get(j);
                        FinFinancingItems items = new FinFinancingItems();
                        items.setId(uidGenerator.getUID());
                        items.setUpperId(financingItems.getId());
                        items.setApplyNo(applyNo);
                        items.setCarId(carId);
                        items.setFinanceItemCode(sonCondition.getFinanceItemCode());
                        items.setFinanceItemName(sonCondition.getFinanceItemName());
                        items.setFinanceItemAmt(sonCondition.getFinanceItemAmt());
                        items.setFinanceItemTotal(sonCondition.getFinanceItemTotal());
                        if(sonCondition.getFinanceItemAmt()!=null && sonCondition.getFinanceItemAmt().compareTo(BigDecimal.ZERO)>0){
                            items.setIsView(Boolean.TRUE);
                        }
                        items.setCreateTime(new Date());
                        list.add(items);
                        sb.append(items.getFinanceItemName()).append("(").append(items.getFinanceItemAmt().setScale(2)).append(")");
                        if (j < sonList.size() - 1) {
                            sb.append("+");
                        }
                    }
                    if (i < itemsList.size() - 1) {
                        sb.append(",");
                    }
                    financingItems.setSonList(list);
                }
                outList.add(financingItems);
            }
        }
        return outList;
    }

    /**
     * 贴息明细
     * @param applyNo
     * @param costId
     * @param costType
     * @param discountType
     * @param totalDiscountAmt
     * @param discountList
     * @return
     */
    public List<FinDiscountDetails> finDiscountDetailsList(String applyNo, Long costId, String costType, String discountType, BigDecimal totalDiscountAmt, List<FinDiscountDetails> discountList){

        List<FinDiscountDetails> outList = new ArrayList<FinDiscountDetails>();
        if (discountList != null && discountList.size() > 0) {
            BigDecimal discountAmt = totalDiscountAmt;
            BigDecimal hundred = new BigDecimal("100");
            for (int i = 0;i<discountList.size();i++) {
                FinDiscountDetails discountCondition = discountList.get(i);
                FinDiscountDetails discountDetails = new FinDiscountDetails();
                discountDetails.setId(uidGenerator.getUID());
                discountDetails.setApplyNo(applyNo);
                discountDetails.setCostId(costId);
                discountDetails.setCostType(costType);
                discountDetails.setDiscountId(discountCondition.getDiscountId());
                discountDetails.setDiscountParty(discountCondition.getDiscountParty());
                discountDetails.setDiscountScale(discountCondition.getDiscountScale());
                discountDetails.setMaxDiscountAmt(discountCondition.getMaxDiscountAmt());
                if(DiscountType.NORMAL.getIndex().equals(discountType)){
                    if(i==discountList.size()-1){
                        discountDetails.setDiscountAmt(discountAmt);
                    }else{
                        // 按比例计算
                        if(ObjectUtil.isNotNull(discountCondition.getDiscountScale())){
                            BigDecimal currentAmt = discountAmt.multiply(discountCondition.getDiscountScale()).divide(hundred,2,BigDecimal.ROUND_HALF_DOWN);
                            if(discountCondition.getMaxDiscountAmt()!=null && currentAmt.compareTo(discountCondition.getMaxDiscountAmt())>=0){
                                currentAmt = discountCondition.getMaxDiscountAmt();
                                hundred = hundred.subtract(discountCondition.getDiscountScale());
                            }
                            discountDetails.setDiscountAmt(currentAmt);
                        }else{
                            if(discountAmt.compareTo(discountCondition.getMaxDiscountAmt())>=0){
                                discountDetails.setDiscountAmt(discountCondition.getMaxDiscountAmt());
                            }else{
                                discountDetails.setDiscountAmt(discountAmt);
                            }
                        }
                        discountAmt = discountAmt.subtract(discountDetails.getDiscountAmt());
                    }
                }else {
                    discountDetails.setDiscountAmt(discountCondition.getDiscountAmt());
                }
                discountDetails.setTotalToTotal(discountCondition.getTotalToTotal());
                outList.add(discountDetails);
            }
        }
        return outList;
    }

    /**
     * 租金调整明细
     * @param applyNo
     * @param inList
     * @return
     */
    public List<FinRentAdjustDetails> finRentAdjustDetailsList(String applyNo, List<FinRentAdjustDetails> inList){

        List<FinRentAdjustDetails> resultList = new ArrayList<FinRentAdjustDetails>();
        if (inList != null && inList.size() > 0) {
            for (FinRentAdjustDetails details : inList) {
                FinRentAdjustDetails rentAdjustDetails = new FinRentAdjustDetails();
                rentAdjustDetails.setId(uidGenerator.getUID());
                rentAdjustDetails.setApplyNo(applyNo);
                rentAdjustDetails.setPeriod(details.getPeriod());
                rentAdjustDetails.setRent(details.getRent());
                resultList.add(rentAdjustDetails);
            }
        }
        return resultList;
    }

    /**
     * 金融产品校验
     * @param condition
     * @return
     */
    public IResponse checkProductInfoForAsset(AssetChangeCondition condition) {

        ProductInfoDTO dto = new ProductInfoDTO();
        dto.setTotalContractAmt(condition.getTotalContractAmt());
        dto.setTotalLoanAmt(condition.getTotalLoanAmt());
        dto.setTotalFirstAmt(condition.getTotalFirstAmt());
        dto.setTotalDiscountAmt(condition.getTotalDiscountAmt());
        dto.setTotalInterest(condition.getTotalInterest());
        dto.setTotalRent(condition.getTotalRent());
        List<CalculatorDTO> list = condition.getCostList();
        for (CalculatorDTO productCondition : list) {
            // 车款费用信息
            if (CostType.CARAMT.getIndex().equals(productCondition.getCostType())) {
                FinCostDetails mainProductInfo = new FinCostDetails();
                BeanUtils.copyProperties(productCondition, mainProductInfo);
                dto.setMainProductInfo(mainProductInfo);
                List<FinFinancingItems> financingItemsList = new ArrayList<FinFinancingItems>();
                List<FinFinancingItems> itemsList = productCondition.getItemsList();
                if (itemsList != null && itemsList.size() > 0) {
                    for (FinFinancingItems itemsCondition : itemsList) {
                        List<FinFinancingItems> sonList = itemsCondition.getSonList();
                        if (sonList != null && sonList.size() > 0) {
                            financingItemsList.addAll(sonList);
                        }
                    }
                    financingItemsList.addAll(itemsList);
                }
                dto.setExtrasInfoList(financingItemsList);
                // 0-主产品和子产品为同一个
                if (WhetherEnum.No.getIndex().equals(productCondition.getIsSubProduct())) {
                    FinCostDetails addProductInfo = new FinCostDetails();
                    addProductInfo.setContractAmt(productCondition.getAddAmt());
                    dto.setAddProductInfo(addProductInfo);
                }
            }
            // 附加贷费用信息
            else if (CostType.AFFIXAMT.getIndex().equals(productCondition.getCostType())) {
                List<FinFinancingItems> financingItemsList = new ArrayList<FinFinancingItems>();
                List<FinFinancingItems> itemsList = productCondition.getItemsList();
                if (itemsList != null && itemsList.size() > 0) {
                    financingItemsList.addAll(itemsList);
                    for (FinFinancingItems itemsCondition : itemsList) {
                        List<FinFinancingItems> sonList = itemsCondition.getSonList();
                        if (sonList != null && sonList.size() > 0) {
                            financingItemsList.addAll(sonList);
                        }
                    }
                }
                FinCostDetails addProductInfo = new FinCostDetails();
                BeanUtils.copyProperties(productCondition, addProductInfo);
                dto.setAddProductInfo(addProductInfo);
                dto.setExtrasInfoList(financingItemsList);
            }
        }
        // 车辆信息
        if(ObjectUtil.isNotNull(condition.getCarInfo())){
            ApplyCarDetails carDetails = finCarDetailsList(condition.getApplyNo(),condition.getCarInfo());
            if (carDetails != null) {
                dto.setCarInfo(carDetails);
            }
        }
        IResponse iResponse = productFeign.checkProductInfo(dto, this.makeProductHeader());
        return iResponse;
    }

    /**
     * 更新修改后对象
     * @param applyNo
     * @param inList
     * @param carInfoCondition
     */
    public ApplyModifyHistory updateBodyContent(String applyNo, List<CalculatorDTO> inList, CarInfoCondition carInfoCondition){

        ApplyModifyHistory applyModifyHistory = this.getModifyHistoryByApplyNo(applyNo);
        BigDecimal hundred = new BigDecimal("100");
        if(applyModifyHistory != null){

            String bodyContent = applyModifyHistory.getBodyContent();
            AssetChangeVo beforeParameter = JSONObject.parseObject(applyModifyHistory.getBeforeParameter()).toJavaObject(AssetChangeVo.class);
            AssetChangeVo afterParameter = null;
            if(StringUtil.isNotBlank(applyModifyHistory.getAfterParameter())){
                afterParameter = JSONObject.parseObject(applyModifyHistory.getAfterParameter()).toJavaObject(AssetChangeVo.class);
            }
            Assert.isTrue(beforeParameter!=null,"留痕数据不存在，请检查");
            BigDecimal oldLoanAmt = beforeParameter.getAddAmt().add(beforeParameter.getLoanAmt());
            BigDecimal oldSalePrice = beforeParameter.getSalePrice();
            BigDecimal oldDownPayScale = beforeParameter.getDownPayScale();
            AssetContentVO vo = new AssetContentVO();
            AssetChangeVo assetChangeVo = new AssetChangeVo();
            // 车辆信息
            List<CaseCarStyleDetailDto> finCarStyleDetailList = new ArrayList<CaseCarStyleDetailDto>();
            List<ApplyCarDetails> finCarDetailsList = new ArrayList<ApplyCarDetails>();
            ApplyCarDetails carDetails = carDetailsService.getCarDetailsByApplyNo(applyNo);
            if(carInfoCondition != null){
                ApplyCarDetails applyCarDetails = this.finCarDetailsList(applyNo,carInfoCondition);
                assetChangeVo.setBrandName(applyCarDetails.getBrandName());
                assetChangeVo.setModelName(applyCarDetails.getModelName());
                assetChangeVo.setStyleName(applyCarDetails.getStyleName());
                if(!carDetails.getStyleId().equals(applyCarDetails.getStyleId())){
                    assetChangeVo.setChangeFlag(WhetherEnum.YES.getIndex());
                }
                applyCarDetails.setId(carDetails.getId());
                finCarDetailsList.add(applyCarDetails);

                CaseCarStyleDetailDto caseCarStyleDetailDTO = new CaseCarStyleDetailDto();
                caseCarStyleDetailDTO.setCarId(applyCarDetails.getId());
                caseCarStyleDetailDTO.setDisplacement(applyCarDetails.getCarDisplacement());
                caseCarStyleDetailDTO.setFuelType(applyCarDetails.getFuelType());
                caseCarStyleDetailDTO.setPassengers(applyCarDetails.getTotalPassenger());
                // 查询款式信息
                TsysVehicleDetail vehicleDetail = tsysVehicleDetailService.getVehicleDetail(applyCarDetails.getStyleId(),applyCarDetails.getModelId());
                if (ObjectUtil.isNotNull(vehicleDetail)) {
                    caseCarStyleDetailDTO.setCarLength(vehicleDetail.getCarLength());
                    caseCarStyleDetailDTO.setEmissionStandard(vehicleDetail.getBlowoff());
                    caseCarStyleDetailDTO.setTotalQuality(vehicleDetail.getWight());
                    caseCarStyleDetailDTO.setIsGreen(vehicleDetail.getIsGreen());
                    caseCarStyleDetailDTO.setCarType(vehicleDetail.getCarType());
                    caseCarStyleDetailDTO.setCarTypeDetail(vehicleDetail.getCarTypeDetail());
                    caseCarStyleDetailDTO.setCarModelId(vehicleDetail.getModelId());
                }
                // 查询车型信息
                TsysVehicleModel tsysVehicleModel = tsysVehicleModelService.getOne(Wrappers.<TsysVehicleModel>query().lambda()
                        .eq(TsysVehicleModel::getBrandCode, applyCarDetails.getBrandId()).eq(TsysVehicleModel::getFullName, applyCarDetails.getModelName()));
                if (ObjectUtil.isNotNull(tsysVehicleModel)) {
                    caseCarStyleDetailDTO.setType(tsysVehicleModel.getType());
                }
                finCarStyleDetailList.add(caseCarStyleDetailDTO);
            }
            // 针对分段保存，已存在车辆信息
            else{
                if(StringUtil.isNotBlank(bodyContent)){
                    JSONObject jsonObject = JSONObject.parseObject(bodyContent);
                    if(jsonObject!=null && jsonObject.containsKey("finCarDetailsList")){
                        finCarDetailsList = jsonObject.getJSONArray("finCarDetailsList").toJavaList(ApplyCarDetails.class);
                        finCarStyleDetailList = jsonObject.getJSONArray("finCarStyleDetailList").toJavaList(CaseCarStyleDetailDto.class);
                    }
                }
                if(ObjectUtil.isNotNull(afterParameter)){
                    assetChangeVo.setBrandName(afterParameter.getBrandName());
                    assetChangeVo.setModelName(afterParameter.getModelName());
                    assetChangeVo.setStyleName(afterParameter.getStyleName());
                    assetChangeVo.setChangeFlag(afterParameter.getChangeFlag());
                }
            }
            vo.setFinCarDetailsList(finCarDetailsList);
            vo.setFinCarStyleDetailList(finCarStyleDetailList);
            // 金融产品
            List<FinCostDetails> finCostDetailsList = new ArrayList<FinCostDetails>();
            List<FinFinancingItems> finFinancingItemsList = new ArrayList<FinFinancingItems>();
            List<FinDiscountDetails> finDiscountDetailsList = new ArrayList<FinDiscountDetails>();
            List<FinRentAdjustDetails> finRentAdjustDetailsList = new ArrayList<FinRentAdjustDetails>();
            ApplyOrderInfo orderInfo = orderInfoService.getOrderInfoByApplyNo(applyNo);
            String channelName = SecurityUtils.getUser().getUserExtInfo().getJSONObject("channelInfo").getString("channelFullName");
            BigDecimal totalContractAmt = BigDecimal.ZERO;
            BigDecimal totalLoanAmt = BigDecimal.ZERO;
            BigDecimal addAmt = BigDecimal.ZERO;
            BigDecimal salePrice = BigDecimal.ZERO;
            BigDecimal downPayScale = BigDecimal.ZERO;
            if(CollectionUtil.isNotEmpty(inList)){
                for (CalculatorDTO condition : inList) {
                    if (CostType.CARAMT.getIndex().equals(condition.getCostType())) {
                        String carAlgorithmType = condition.getAlgorithmType();
                        if(CalculateType.arbitraryloan.getValue().equals(carAlgorithmType)){
                            List<FinRentAdjustDetails> rentList = condition.getRentList();
                            Assert.isTrue(rentList!=null && rentList.size()>0,"任意贷产品必须调整租金才能保存");
                        }
                    }
                    // 融资信息
                    FinCostDetails costDetails = costDetailsService.getApplyCostDetails(applyNo,condition.getCostType());
                    FinCostDetails finCostDetails = finCostDetailsList(applyNo,condition);
                    if (CostType.CARAMT.getIndex().equals(finCostDetails.getCostType())) {
                        assetChangeVo.setLoanAmt(finCostDetails.getLoanAmt().setScale(2));
                        assetChangeVo.setLoanTerm(finCostDetails.getLoanTerm());
                        downPayScale = finCostDetails.getDownPayScale().setScale(2);
                        assetChangeVo.setDownPayScale(downPayScale);
                        assetChangeVo.setFinanceItemName(finCostDetails.getProductName());
                        totalContractAmt = totalContractAmt.add(finCostDetails.getContractAmt());
                        if(WhetherEnum.No.getIndex().equals(finCostDetails.getIsSubProduct())){
                            addAmt = finCostDetails.getAddAmt();
                            assetChangeVo.setAddAmt(finCostDetails.getAddAmt().multiply(hundred.subtract(finCostDetails.getDownPayScale())).divide(hundred,2,BigDecimal.ROUND_HALF_DOWN));
                            totalContractAmt = totalContractAmt.add(addAmt);
                        }
                        salePrice = finCostDetails.getContractAmt();
                        assetChangeVo.setSalePrice(salePrice);
                    }else if(CostType.AFFIXAMT.getIndex().equals(finCostDetails.getCostType())){
                        assetChangeVo.setAddAmt(finCostDetails.getLoanAmt().setScale(2));
                        assetChangeVo.setAddLoanTerm(finCostDetails.getLoanTerm());
                        assetChangeVo.setAddDownPayScale(finCostDetails.getDownPayScale().setScale(2));
                        addAmt = finCostDetails.getContractAmt();
                        totalContractAmt = totalContractAmt.add(finCostDetails.getContractAmt());
                    }
                    if(costDetails != null){
                        finCostDetails.setId(costDetails.getId());
                    }else{
                        finCostDetails.setId(uidGenerator.getUID());
                    }
                    finCostDetails.setCarId(carDetails.getId());
                    finCostDetailsList.add(finCostDetails);
                    totalLoanAmt = totalLoanAmt.add(condition.getLoanAmt());
                    // 融资项目
                    StringBuilder sb = new StringBuilder("总额:"+addAmt.setScale(2)+",");
                    List<FinFinancingItems> itemsList = finFinancingItemsList(applyNo,carDetails.getId(),condition.getItemsList(),sb);
                    if(itemsList!=null && itemsList.size()>0){
                        assetChangeVo.setAddLoanName(sb.toString());
                        finFinancingItemsList.addAll(itemsList);
                    }
                    // 贴息明细
                    List<FinDiscountDetails> discountList = condition.getDiscountList();
                    if(discountList!=null && discountList.size()>0){
                        for(FinDiscountDetails discountDetails:discountList){
                            if("0".equals(discountDetails.getDiscountId())){// 0-厂商
                                discountDetails.setDiscountParty(ApplyUtils.dicData(discountDetails.getDiscountParty(), "subsidyNameType"));
                            }else if("1".equals(discountDetails.getDiscountId())){// 1-合作商
                                discountDetails.setDiscountParty(channelName);
                            }else if("2".equals(discountDetails.getDiscountId())){// 2-车商
                                discountDetails.setDiscountParty(orderInfo.getDealerName());
                            }
                        }
                    }
                    if(costDetails != null){
                        List<FinDiscountDetails> discountDetailsList = finDiscountDetailsList(applyNo,costDetails.getId(),costDetails.getCostType(),condition.getDiscountType(), condition.getDiscountAmt(),discountList);
                        if(CollectionUtil.isNotEmpty(discountDetailsList)){
                            finDiscountDetailsList.addAll(discountDetailsList);
                        }
                    }
                    // 租金调整明细
                    finRentAdjustDetailsList.addAll(finRentAdjustDetailsList(applyNo,condition.getRentList()));
                }
                List<FinCostDetails> oldCostDetailsList = JSONObject.parseObject(applyModifyHistory.getHistoryContent()).getJSONArray("finCostDetailsList").toJavaList(FinCostDetails.class);
                if(1==inList.size() && 1==oldCostDetailsList.size()){
                    if(salePrice.compareTo(oldSalePrice)>=0){
                        Assert.isTrue(totalLoanAmt.compareTo(oldLoanAmt)<=0,"最大贷额为"+oldLoanAmt);
                    }else{
                        Assert.isTrue(downPayScale.compareTo(oldDownPayScale)>=0,"最低首付比例"+oldDownPayScale);
                    }
                }else{
                    Assert.isTrue(totalLoanAmt.compareTo(oldLoanAmt)<=0,"最大贷额为"+oldLoanAmt);
                }
                assetChangeVo.setContractAmt(totalContractAmt.setScale(2));
                assetChangeVo.setChangeFlag(WhetherEnum.No.getIndex());
            }
            // 针对分段保存，已存在金融产品
            else{
                if(StringUtil.isNotBlank(bodyContent)){
                    JSONObject jsonObject = JSONObject.parseObject(bodyContent);
                    if(jsonObject!=null){
                        if(jsonObject.containsKey("finCostDetailsList")){
                            finCostDetailsList = jsonObject.getJSONArray("finCostDetailsList").toJavaList(FinCostDetails.class);
                        }
                        if(jsonObject.containsKey("finFinancingItemsList")){
                            finFinancingItemsList = jsonObject.getJSONArray("finFinancingItemsList").toJavaList(FinFinancingItems.class);
                        }
                        if(jsonObject.containsKey("finDiscountDetailsList")){
                            finDiscountDetailsList = jsonObject.getJSONArray("finDiscountDetailsList").toJavaList(FinDiscountDetails.class);
                        }
                        if(jsonObject.containsKey("finRentAdjustDetailsList")){
                            finRentAdjustDetailsList =  jsonObject.getJSONArray("finRentAdjustDetailsList").toJavaList(FinRentAdjustDetails.class);
                        }
                    }
                }
                if(ObjectUtil.isNotNull(afterParameter)){
                    assetChangeVo.setFinanceItemName(afterParameter.getFinanceItemName());
                    assetChangeVo.setDownPayScale(afterParameter.getDownPayScale());
                    assetChangeVo.setAddDownPayScale(afterParameter.getAddDownPayScale());
                    assetChangeVo.setLoanTerm(afterParameter.getLoanTerm());
                    assetChangeVo.setAddLoanTerm(afterParameter.getAddLoanTerm());
                    assetChangeVo.setLoanAmt(afterParameter.getLoanAmt());
                    assetChangeVo.setAddAmt(afterParameter.getAddAmt());
                    assetChangeVo.setContractAmt(afterParameter.getContractAmt());
                    assetChangeVo.setAddLoanName(afterParameter.getAddLoanName());
                    assetChangeVo.setSalePrice(afterParameter.getSalePrice());
                }
            }
            vo.setFinCostDetailsList(finCostDetailsList);
            vo.setFinFinancingItemsList(finFinancingItemsList);
            vo.setFinDiscountDetailsList(finDiscountDetailsList);
            vo.setFinRentAdjustDetailsList(finRentAdjustDetailsList);
            // 更新操作记录表
            ApplyOprRecord applyOprRecord = this.assertChange(applyNo,AssetChangeEnums.DRAFT.getCode());
            applyModifyHistory.setAfterParameter(JSONObject.toJSONString(assetChangeVo));
            applyModifyHistory.setBodyContent(JSONObject.toJSONString(vo));
            applyModifyHistory.setOprId(applyOprRecord.getId());
            this.updateById(applyModifyHistory);
        }
        return applyModifyHistory;
    }

    /**
     * 保存操作记录
     *
     * @param applyNo
     * @param applyStatus
     */
    public ApplyOprRecord assertChange(String applyNo,String applyStatus) {

        //查询客户信息
        ApplyCustBaseInfo custBaseInfo = applyCustBaseInfoService.getCustBaseInfo(applyNo, ApplyConstants.PRINCIPAL_BORROWER);
        //如果存在资产变更操作记录
        List<ApplyOprRecord> recordList = applyOprRecordService.list(Wrappers.<ApplyOprRecord>query().lambda().eq(ApplyOprRecord::getApplyNo, applyNo)
                .eq(ApplyOprRecord::getApplyType, String.valueOf(OrderOprType.ASSERTCHANGE)).orderByDesc(ApplyOprRecord::getCreateTime));
        // 第一次保存
        if (CollectionUtil.isEmpty(recordList)) {
            ApplyOprRecord applyOprRecord = new ApplyOprRecord();
            applyOprRecord.setCertNo(custBaseInfo.getCertNo());
            applyOprRecord.setCustName(custBaseInfo.getCustName());
            applyOprRecord.setApplyType(OrderOprType.ASSERTCHANGE);
            applyOprRecord.setApplyNo(applyNo);
            applyOprRecord.setApplyStatus(applyStatus);
            applyOprRecordService.save(applyOprRecord);
            return applyOprRecord;
        }
        // 第二次保存
        else {
            ApplyOprRecord oprRecord = recordList.get(0);
            if(oprRecord.getApproveDate() != null && AssetChangeEnums.DRAFT.getCode().equals(applyStatus)){
                ApplyOprRecord applyOprRecord = new ApplyOprRecord();
                applyOprRecord.setCertNo(custBaseInfo.getCertNo());
                applyOprRecord.setCustName(custBaseInfo.getCustName());
                applyOprRecord.setApplyType(OrderOprType.ASSERTCHANGE);
                applyOprRecord.setApplyNo(applyNo);
                applyOprRecord.setApplyStatus(applyStatus);
                applyOprRecordService.save(applyOprRecord);
                return applyOprRecord;
            }else{
                oprRecord.setApplyStatus(applyStatus);
                applyOprRecordService.updateById(oprRecord);
                return oprRecord;
            }
        }
    }

    /**
     * 金融产品-资产变更
     * @param condition
     */
    public FinanceCalculatorBean getFinancialProductsForAssetChange(FinanceCalculatorBean vo,CalculatorDTO condition){

        ApplyModifyHistory modifyHistory = this.getModifyHistoryByApplyNo(condition.getApplyNo());
        if(modifyHistory != null){
            String bodyContent = modifyHistory.getBodyContent();
            if(StringUtil.isNotBlank(bodyContent)){
                vo = new FinanceCalculatorBean();
                JSONObject jsonObject = JSONObject.parseObject(bodyContent);
                List<FinCostDetails> finCostDetailsList = jsonObject.getJSONArray("finCostDetailsList").toJavaList(FinCostDetails.class);
                AssetChangeVo afterParameter = JSONObject.parseObject(modifyHistory.getAfterParameter()).toJavaObject(AssetChangeVo.class);
                Assert.isTrue(afterParameter!=null,"请保存资产变更信息之后再查询");
                vo.setChangeFlag(afterParameter.getChangeFlag());
                if (finCostDetailsList == null || finCostDetailsList.size() == 0) {
                    vo.setSalePrice(afterParameter.getSalePrice());
                    return vo;
                }
                // 融资项目
                List<FinFinancingItems> finFinancingItemsList = null;
                if(jsonObject.containsKey("finFinancingItemsList")){
                    finFinancingItemsList = jsonObject.getJSONArray("finFinancingItemsList").toJavaList(FinFinancingItems.class);
                }
                // 贴息明细
                List<FinDiscountDetails> finDiscountDetailsList = null;
                if(jsonObject.containsKey("finDiscountDetailsList")){
                    finDiscountDetailsList = jsonObject.getJSONArray("finDiscountDetailsList").toJavaList(FinDiscountDetails.class);
                }
                String isAdditional = WhetherEnum.No.getIndex();
                String isSubProduct = null;
                // 汇总金额
                BigDecimal totalContractAmt = BigDecimal.ZERO;
                BigDecimal totalLoanAmt = BigDecimal.ZERO;
                BigDecimal totalFirstAmt = BigDecimal.ZERO;
                BigDecimal totalDiscountAmt = BigDecimal.ZERO;
                BigDecimal totalInterest = BigDecimal.ZERO;
                BigDecimal totalRent = BigDecimal.ZERO;
                BigDecimal totalTailAmt = BigDecimal.ZERO;
                BigDecimal salePrice = BigDecimal.ZERO;
                for (FinCostDetails costDetails : finCostDetailsList) {
                    totalContractAmt = totalContractAmt.add(costDetails.getContractAmt() == null ? BigDecimal.ZERO : costDetails.getContractAmt());
                    totalLoanAmt = totalLoanAmt.add(costDetails.getLoanAmt() == null ? BigDecimal.ZERO : costDetails.getLoanAmt());
                    totalFirstAmt = totalFirstAmt.add(costDetails.getDownPayAmt() == null ? BigDecimal.ZERO : costDetails.getDownPayAmt());
                    totalDiscountAmt = totalDiscountAmt.add(costDetails.getDiscountAmt() == null ? BigDecimal.ZERO : costDetails.getDiscountAmt());
                    totalRent = totalRent.add(costDetails.getTotalRent() == null ? BigDecimal.ZERO : costDetails.getTotalRent());
                    totalInterest = totalInterest.add(costDetails.getTotalInterest() == null ? BigDecimal.ZERO : costDetails.getTotalInterest());
                    totalTailAmt = totalTailAmt.add(costDetails.getTailPayAmt() == null ? BigDecimal.ZERO : costDetails.getTailPayAmt());
                    // 车款
                    if(CostType.CARAMT.getIndex().equals(costDetails.getCostType())){
                        FinanceInfoBean carProductInfo = new FinanceInfoBean();
                        BigDecimal monthlyRate = costDetailsService.getMonthlyRate(costDetails.getLoanTerm(),costDetails.getMonthPayAmt(),costDetails.getLoanAmt());
                        costDetails.setMonthlyRate(monthlyRate);
                        BigDecimal myriadCoefficient = costDetailsService.getMyriadCoefficient(costDetails.getMonthPayAmt(),costDetails.getLoanAmt());
                        costDetails.setMyriadCoefficient(myriadCoefficient);
                        carProductInfo.setCostInfo(costDetails);
                        carProductInfo.setIsTail(costDetails.getIsTail());
                        carProductInfo.setDiscountOption(costDetails.getDiscountType());
                        isSubProduct = costDetails.getIsSubProduct();
                        salePrice = costDetails.getContractAmt();
                        // 有子产品融资项目
                        if (WhetherEnum.No.getIndex().equals(isSubProduct)) {
                            if(finFinancingItemsList!=null && finFinancingItemsList.size()>0){
                                carProductInfo.setItemList(finFinancingItemsList);
                                isAdditional = WhetherEnum.YES.getIndex();
                                carProductInfo.setItemList(finFinancingItemsList);
                            }
                        }
                        // 贴息
                        List<FinDiscountDetails> discountList = new ArrayList<FinDiscountDetails>();
                        if(finDiscountDetailsList!=null && finDiscountDetailsList.size()>0){
                            for(FinDiscountDetails discountDetails:finDiscountDetailsList){
                                if(CostType.CARAMT.getIndex().equals(discountDetails.getCostType())){
                                    discountList.add(discountDetails);
                                }
                            }
                        }
                        carProductInfo.setDiscountList(discountList);
                        vo.setCarProductInfo(carProductInfo);
                    }
                    // 附加贷
                    else if(CostType.AFFIXAMT.getIndex().equals(costDetails.getCostType())){
                        FinanceInfoBean addProductInfo = new FinanceInfoBean();
                        BigDecimal monthlyRate = costDetailsService.getMonthlyRate(costDetails.getLoanTerm(),costDetails.getMonthPayAmt(),costDetails.getLoanAmt());
                        costDetails.setMonthlyRate(monthlyRate);
                        BigDecimal myriadCoefficient = costDetailsService.getMyriadCoefficient(costDetails.getMonthPayAmt(),costDetails.getLoanAmt());
                        costDetails.setMyriadCoefficient(myriadCoefficient);
                        addProductInfo.setCostInfo(costDetails);
                        addProductInfo.setIsTail(costDetails.getIsTail());
                        addProductInfo.setDiscountOption(costDetails.getDiscountType());
                        // 贴息
                        List<FinDiscountDetails> discountList = new ArrayList<FinDiscountDetails>();
                        if(finDiscountDetailsList!=null && finDiscountDetailsList.size()>0){
                            for(FinDiscountDetails discountDetails:finDiscountDetailsList){
                                if(CostType.AFFIXAMT.getIndex().equals(discountDetails.getCostType())){
                                    discountList.add(discountDetails);
                                }
                            }
                        }
                        addProductInfo.setDiscountList(discountList);
                        isSubProduct = costDetails.getIsSubProduct();
                        if(finFinancingItemsList!=null && finFinancingItemsList.size()>0){
                            addProductInfo.setItemList(finFinancingItemsList);
                            isAdditional = WhetherEnum.YES.getIndex();
                        }
                        addProductInfo.setItemList(finFinancingItemsList);
                        vo.setAddProductInfo(addProductInfo);
                    }
                }
                vo.setIsAdditional(isAdditional);
                vo.setIsSubProduct(isSubProduct);
                // 合计信息
                vo.setTotalContractAmt(totalContractAmt);
                vo.setTotalLoanAmt(totalLoanAmt);
                vo.setTotalFirstAmt(totalFirstAmt);
                vo.setTotalDiscountAmt(totalDiscountAmt);
                vo.setTotalInterest(totalInterest);
                vo.setTotalRent(totalRent);
                vo.setTotalTailAmt(totalTailAmt);
                vo.setSalePrice(salePrice);
                // 租金调整
                if(jsonObject.containsKey("finRentAdjustDetailsList")){
                    List<FinRentAdjustDetails> finRentAdjustDetailsList =  jsonObject.getJSONArray("finRentAdjustDetailsList").toJavaList(FinRentAdjustDetails.class);
                    if(finRentAdjustDetailsList!=null && finRentAdjustDetailsList.size()>0){
                        vo.setRentList(finRentAdjustDetailsList);
                    }
                }
            }
        }
        return vo;
    }

    /**
     * 车辆信息-资产变更
     * @param vo
     * @param condition
     */
    public CarInfoVO getCarInfoForAssetChange(CarInfoVO vo, ApplyReportCondition condition){

        ApplyModifyHistory modifyHistory = this.getModifyHistoryByApplyNo(condition.getApplyNo());
        if(modifyHistory != null){
            String bodyContent = modifyHistory.getBodyContent();
            if(StringUtil.isNotBlank(bodyContent)){
                JSONObject jsonObject = JSONObject.parseObject(bodyContent);
                List<ApplyCarDetails> finCarDetailsList = jsonObject.getJSONArray("finCarDetailsList").toJavaList(ApplyCarDetails.class);
                if (finCarDetailsList!=null && finCarDetailsList.size()>0){
                    ApplyCarDetails carDetails = finCarDetailsList.get(0);
                    if (carDetails != null) {
                        vo = new CarInfoVO();
                        vo.setApplyNo(carDetails.getApplyNo());
                        if (CarNature.NEW_CAR.getIndex().equals(condition.getCarNature())) {
                            ApplyCarDetails newCarInfo = new ApplyCarDetails();
                            newCarInfo.setId(carDetails.getId());
                            newCarInfo.setBrandId(carDetails.getBrandId());
                            newCarInfo.setBrandName(carDetails.getBrandName());
                            newCarInfo.setModelId(carDetails.getModelId());
                            newCarInfo.setModelName(carDetails.getModelName());
                            newCarInfo.setStyleId(carDetails.getStyleId());
                            newCarInfo.setStyleName(carDetails.getStyleName());
                            newCarInfo.setGuidePrice(carDetails.getGuidePrice());
                            newCarInfo.setSalePrice(carDetails.getSalePrice());
                            newCarInfo.setPurchaseProvince(carDetails.getPurchaseProvince());
                            newCarInfo.setPurchaseCity(carDetails.getPurchaseCity());
                            newCarInfo.setLicenseProvince(carDetails.getLicenseProvince());
                            newCarInfo.setLicenseCity(carDetails.getLicenseCity());
                            vo.setNewCarInfo(newCarInfo);
                        } else if (CarNature.PLATE_CAR.getIndex().equals(condition.getCarNature())) {
                            ApplyCarDetails licenseCarInfo = new ApplyCarDetails();
                            licenseCarInfo.setId(carDetails.getId());
                            licenseCarInfo.setCarVin(carDetails.getCarVin());
                            licenseCarInfo.setBrandId(carDetails.getBrandId());
                            licenseCarInfo.setBrandName(carDetails.getBrandName());
                            licenseCarInfo.setModelId(carDetails.getModelId());
                            licenseCarInfo.setModelName(carDetails.getModelName());
                            licenseCarInfo.setStyleId(carDetails.getStyleId());
                            licenseCarInfo.setStyleName(carDetails.getStyleName());
                            licenseCarInfo.setGuidePrice(carDetails.getGuidePrice());
                            licenseCarInfo.setSalePrice(carDetails.getSalePrice());
                            licenseCarInfo.setCarFunction(carDetails.getCarFunction());
                            licenseCarInfo.setEngineNo(carDetails.getEngineNo());
                            licenseCarInfo.setPurchaseProvince(carDetails.getPurchaseProvince());
                            licenseCarInfo.setPurchaseCity(carDetails.getPurchaseCity());
                            licenseCarInfo.setLicenseProvince(carDetails.getLicenseProvince());
                            licenseCarInfo.setLicenseCity(carDetails.getLicenseCity());
                            licenseCarInfo.setMileage(carDetails.getMileage());
                            licenseCarInfo.setVehicleMadeDate(carDetails.getVehicleMadeDate());
                            licenseCarInfo.setFirstLandingDate(carDetails.getFirstLandingDate());
                            licenseCarInfo.setLicensePlate(carDetails.getLicensePlate());
                            licenseCarInfo.setFirstLicenseProvince(carDetails.getFirstLicenseProvince());
                            licenseCarInfo.setFirstLicenseCity(carDetails.getFirstLicenseCity());
                            licenseCarInfo.setFirstLicenseAddress(carDetails.getFirstLicenseAddress());
                            licenseCarInfo.setCarBelongs(carDetails.getCarBelongs());
                            licenseCarInfo.setBelongsCertNo(carDetails.getBelongsCertNo());
                            licenseCarInfo.setInvoiceTime(carDetails.getInvoiceTime());
                            licenseCarInfo.setEvaluatingPrice(carDetails.getEvaluatingPrice());
                            licenseCarInfo.setIsFirstLicense(carDetails.getIsFirstLicense());
                            vo.setLicenseCarInfo(licenseCarInfo);
                        } else {
                            ApplyCarDetails oldCarInfo = new ApplyCarDetails();
                            BeanUtils.copyProperties(carDetails, oldCarInfo);
                            vo.setOldCarInfo(oldCarInfo);
                        }
                    }
                }
            }
        }
        return vo;
    }

}
